using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class KitchenImportEntriesController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();


        public ActionResult Confirm(int id)
        {
            KitchenImportEntry kitchenImportEntry = context.KitchenImportEntries.FirstOrDefault(x => x.KitchenImportEntryID == id);
            kitchenImportEntry.IsConfirmed = true;
            foreach (var item in kitchenImportEntry.StoreToKitchenEntry.StoreToKitchenEntryItems)
            {
                KitchenProductStock newKitchenProductStock = new KitchenProductStock
                {
                    StoreToKitchenEntryItemID = item.StoreToKitchenEntryItemID,
                    OriginalQuantity = item.Quantity / item.Unit.ConvertingFactorToMainUnit,
                    CurrentQuantity = item.Quantity / item.Unit.ConvertingFactorToMainUnit,
                    Cost = item.Cost
                };
                context.KitchenProductStocks.Add(newKitchenProductStock);
                context.SaveChanges();
            }
            return RedirectToAction("Index", "KitchenProductStocks");
        }

        //
        // GET: /KitchenImportEntries/

        public ViewResult Index()
        {
            return View(context.KitchenImportEntries.Include(kitchenimportentry => kitchenimportentry.StoreToKitchenEntry).ToList());
        }

        //
        // GET: /KitchenImportEntries/Details/5

        public ViewResult Details(int id)
        {
            KitchenImportEntry kitchenimportentry = context.KitchenImportEntries.Single(x => x.KitchenImportEntryID == id);
            return View(kitchenimportentry);
        }

        //
        // GET: /KitchenImportEntries/Create

        public ActionResult Create()
        {
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            return View();
        } 

        //
        // POST: /KitchenImportEntries/Create

        [HttpPost]
        public ActionResult Create(KitchenImportEntry kitchenimportentry)
        {
            if (ModelState.IsValid)
            {
                context.KitchenImportEntries.Add(kitchenimportentry);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            return View(kitchenimportentry);
        }
        
        //
        // GET: /KitchenImportEntries/Edit/5
 
        public ActionResult Edit(int id)
        {
            KitchenImportEntry kitchenimportentry = context.KitchenImportEntries.Single(x => x.KitchenImportEntryID == id);
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            return View(kitchenimportentry);
        }

        //
        // POST: /KitchenImportEntries/Edit/5

        [HttpPost]
        public ActionResult Edit(KitchenImportEntry kitchenimportentry)
        {
            if (ModelState.IsValid)
            {
                context.Entry(kitchenimportentry).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            return View(kitchenimportentry);
        }

        //
        // GET: /KitchenImportEntries/Delete/5
 
        public ActionResult Delete(int id)
        {
            KitchenImportEntry kitchenimportentry = context.KitchenImportEntries.Single(x => x.KitchenImportEntryID == id);
            return View(kitchenimportentry);
        }

        //
        // POST: /KitchenImportEntries/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            KitchenImportEntry kitchenimportentry = context.KitchenImportEntries.Single(x => x.KitchenImportEntryID == id);
            context.KitchenImportEntries.Remove(kitchenimportentry);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}